import { EcsEntity as number } from "./EcsEntity";


/**
 * 注册组件的类型装饰器
 */
export function RegComponentType() {
	return function (target) {
		const id = EcsComponent.__next_gen_id++

		Object.defineProperty(
			target,
			'__compId',
			{
				value: id,
				writable: false,
				enumerable: false
			}
		)
		if (!EcsComponent.__types[id]) {
			EcsComponent.__types[id] = target;
		}
	}
}


export class EcsComponent {
	public enable: boolean = true;
	public ownerId: number = 0;

	public static __next_gen_id = 0
	public static __types: { [id: number]: new (...args: any[]) => EcsComponent } = {};

	static getIdFromClz<T extends (EcsComponent)>(compClz: new () => T): number {
		return (compClz as any).__compId
	}


	static getId(comp: EcsComponent): number {
		return (comp.constructor as any).__compId
	}

	public static getComponentType(id: number): new (...args: any[]) => EcsComponent {
		return this.__types[id];
	}

	/**重置组件id生成器,在一个ECS组件定义文件中开始注册组件前调用 */
	public static resetGenId() {
		this.__next_gen_id = 0
	}
}